package com.ethink.platform.controller.warehouse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.framework.common.web.BaseController;
import com.ethink.framework.common.web.context.SPRequestContextHolder;
import com.ethink.framework.common.web.response.CommonResponse;
import com.ethink.platform.controller.order.info.ExportInfo;
import com.ethink.platform.controller.order.info.MateInventorySaleChannelChangeLogDetailImportInfo;
import com.ethink.platform.manager.ExportManager;
import com.ethink.platform.manager.MateInventorySaleChannelChangeLogManager;
import com.ethink.warehouse.api.MateInventorySaleChannelChangeLogFacade;
import com.ethink.warehouse.api.info.MateInventorySaleChannelChangeLogInfo;
import com.ethink.warehouse.api.info.MateInventorySaleChannelChangeLogModeInfo;
import com.ethink.warehouse.api.request.MateInventorySaleChannelChangeLogAddRequest;
import com.ethink.warehouse.api.request.MateInventorySaleChannelChangeLogQueryRequest;
import com.ethink.warehouse.api.request.MateInventorySaleChannelChangeLogUpdateRequest;
import com.ethink.warehouse.api.response.MateInventorySaleChannelChangeLogGetDetailResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 库存货权变更 Controller
 *
 * @author default
 * @date 2023-01-06
 */
@Tag(name = "MateInventorySaleChannelChangeLogController", description = "库存货权变更 Controller")
@RestController
@Slf4j
@RequestMapping("platform-server/v1/warehouse-service")
public class MateInventorySaleChannelChangeLogController extends BaseController {

    @Autowired
    private MateInventorySaleChannelChangeLogFacade mateInventorySaleChannelChangeLogFacade;

    @Autowired
    private MateInventorySaleChannelChangeLogManager mateInventorySaleChannelChangeLogManager;

    @Autowired
    private ExportManager exportManager;

    @Operation(summary = "分页查询")
    @GetMapping("/mateInventorySaleChannelChangeLog/query")
    public CommonResponse<List<MateInventorySaleChannelChangeLogInfo>> queryMateInventorySaleChannelChangeLog(MateInventorySaleChannelChangeLogQueryRequest request) {
        return succeedWithPage(mateInventorySaleChannelChangeLogFacade.queryMateInventorySaleChannelChangeLog(request));
    }

    @Operation(summary = "分页查询导出")
    @GetMapping("/mateInventorySaleChannelChangeLog/export")
    public CommonResponse<ExportInfo> exportMateInventorySaleChannelChangeLog(MateInventorySaleChannelChangeLogQueryRequest request) {
        return succeedWith(exportManager.exportMateInventorySaleChannelChangeLog(request));
    }

    @Operation(summary = "货品模式分页查询")
    @GetMapping("/mateInventorySaleChannelChangeLog/queryMode")
    public CommonResponse<List<MateInventorySaleChannelChangeLogModeInfo>> queryMateInventorySaleChannelChangeModeLog(MateInventorySaleChannelChangeLogQueryRequest request) {
        Page<MateInventorySaleChannelChangeLogModeInfo> mateInventorySaleChannelChangeLogModeInfoPage = mateInventorySaleChannelChangeLogFacade.queryMateInventorySaleChannelChangeModeLog(request);
        return succeedWithPage(mateInventorySaleChannelChangeLogModeInfoPage);
    }

    @Operation(summary = "货品模式分页查询导出")
    @GetMapping("/mateInventorySaleChannelChangeLog/exportMode")
    public CommonResponse<ExportInfo> exportMateInventorySaleChannelChangeModeLog(MateInventorySaleChannelChangeLogQueryRequest request) {
        return succeedWith(exportManager.exportMateInventorySaleChannelChangeModeLog(request));
    }

    @Operation(summary = "详情查询")
    @GetMapping("/mateInventorySaleChannelChangeLog/get/{mateInventorySaleChannelChangeLogId}")
    public CommonResponse<MateInventorySaleChannelChangeLogGetDetailResponse> getMateInventorySaleChannelChangeLogById(@PathVariable("mateInventorySaleChannelChangeLogId") Long mateInventorySaleChannelChangeLogId) {
        return succeedWith(mateInventorySaleChannelChangeLogFacade.getMateInventorySaleChannelChangeLogById(mateInventorySaleChannelChangeLogId));
    }

    @Operation(summary = "明细导入")
    @PostMapping("/mateInventorySaleChannelChangeLog/detailImport")
    public CommonResponse<List<MateInventorySaleChannelChangeLogDetailImportInfo>> importMateInventorySaleChannelChangeLogDetail(
            @RequestParam("subWarehouseCode") String subWarehouseCode,
            @RequestParam("saleSubChannel") String saleSubChannel,
            @RequestParam("file") MultipartFile file
    ) {
        if (StringUtils.isBlank(subWarehouseCode) || StringUtils.isBlank(saleSubChannel)) {
            throw new BusinessException(CommonRespCode.PARAM_ERR, "请先选择仓库和渠道");
        }
        return succeedWith(mateInventorySaleChannelChangeLogManager.importMateInventorySaleChannelChangeLogDetail(subWarehouseCode, saleSubChannel, file));
    }

    @Operation(summary = "新增")
    @PostMapping("/mateInventorySaleChannelChangeLog/add")
    public CommonResponse addMateInventorySaleChannelChangeLog(@RequestBody MateInventorySaleChannelChangeLogAddRequest request) {
        request.setCreateUserCode(SPRequestContextHolder.getCurrentSystemUserLoginName());
        request.setCreateUserName(SPRequestContextHolder.getCurrentSystemUserRealName());
        mateInventorySaleChannelChangeLogFacade.addMateInventorySaleChannelChangeLog(request);
        return succeed();
    }

    @Operation(summary = "更新")
    @PutMapping("/mateInventorySaleChannelChangeLog/update")
    public CommonResponse updateMateInventorySaleChannelChangeLog(@RequestBody MateInventorySaleChannelChangeLogUpdateRequest request) {
        mateInventorySaleChannelChangeLogFacade.updateMateInventorySaleChannelChangeLog(request);
        return succeed();
    }

    @Operation(summary = "审核通过")
    @PutMapping("/mateInventorySaleChannelChangeLog/approved/{id}")
    public CommonResponse approvedMateInventorySaleChannelChangeLog(@PathVariable("id") Long id) {
        mateInventorySaleChannelChangeLogFacade.approvedMateInventorySaleChannelChangeLog(id);
        return succeed();
    }

    @Operation(summary = "审核拒绝")
    @PutMapping("/mateInventorySaleChannelChangeLog/refuse/{id}")
    public CommonResponse refuseMateInventorySaleChannelChangeLog(@PathVariable("id") Long id) {
        mateInventorySaleChannelChangeLogFacade.refuseMateInventorySaleChannelChangeLog(id);
        return succeed();
    }

    @Operation(summary = "删除")
    @PutMapping("/mateInventorySaleChannelChangeLog/delete/{mateInventorySaleChannelChangeLogId}")
    public CommonResponse deleteMateInventorySaleChannelChangeLog(@PathVariable("mateInventorySaleChannelChangeLogId") Long mateInventorySaleChannelChangeLogId) {
        mateInventorySaleChannelChangeLogFacade.deleteMateInventorySaleChannelChangeLog(mateInventorySaleChannelChangeLogId);
        return succeed();
    }

}